package com.xtkj.superclub_fastdfs.controller;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.xtkj.superclub_fastdfs.entity.Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastdfs")
public class FastDfsController {

    @Autowired
    private Bean bean;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    //上传文件
    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST)
    public String uploadFile(@RequestParam("file") MultipartFile file) throws Exception{

        String ext_Name = file.getOriginalFilename().split("\\.")[1];
        System.out.println("ext_Name is "+ext_Name);

        String file_Name = file.getOriginalFilename().split("\\.")[0];
        System.out.println("file_Name is "+file_Name);

        /*byte[] bytes = null;

        try {
            bytes = file.getBytes();
        } catch (exception e) {
            e.printStackTrace();
        }*/

        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(),file.getSize(),ext_Name,null);

        System.out.println(storePath.getFullPath());

        return bean.getIp()+":"+bean.getPort()+"/"+storePath.getFullPath();
    }

    //删除文件
    @RequestMapping(value = "/deleteFile",method = RequestMethod.DELETE)
    public void deleteFile(@RequestParam("filePath") String filePath){
        try {
            fastFileStorageClient.deleteFile(filePath);
            System.out.println("删除 "+filePath+" 成功。。");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

}
